package frame.exec 
{
	import data.DataMonitor;
	import debug.ASSERT;
	import flash.geom.Point;
	import frame.IFrame;
	import nsdecl.ns_interactive_slot;
	import nsdecl.ns_vname_decl;
	import ui.basePannel;
	import ui.ui_button;
	import ui.ui_combo_box;
	import ui.uiWin;
	import ui.utils.dialogProperty;
	import utils.range;
	import vndecl.*;
	
	/**
	 * ...
	 * @author anod
	 */
	public class SetDateRange extends basePannel implements IFrame 
	{
		use namespace ns_vname_decl;
		use namespace ns_interactive_slot;
		
		private var mFrame:FSetExecDate;
		
		public function SetDateRange( frame:FSetExecDate ) 
		{
			mFrame = frame;
			addChild( mFrame );
			
			super();
			setupInteractive();
			setupDataMonitor();
		}
		
		/* INTERFACE frame.IFrame */
		
		public function setupInteractive():void 
		{
			iYearStart.onselect     = "startDayByYear";
			iMonthStart.onselect    = "startDayByMonth";
			iYearEnd.onselect       = "endMonthByYear";
			iMonthEnd.onselect      = "endDateByMonth";
			iNextButton.click       = "setTimePanel";
			iCancelButton.click     = "switchChild";
		}
		
		public function setupDataMonitor():void 
		{
			
		}
		
		// UI回调
		//----------------------------------------------
		/** 更新日期 */
		ns_interactive_slot function startDayByMonth( index:int ):void
		{
			var day:Date;
			if ( index == iMonthStart.data.length )
			{
				day = new Date( 1 + int(iYearStart.value), 0, 1 );
				day.setTime( day.time - 1000 );
			}
			else
			{
				day = new Date( iYearStart.value, iMonthStart.currentKey + 1, 1 );
				day.setTime( day.time - 1000 );
			}
			iDayStart.data = range( day.date, 1 ).join(",");
			adjustEndRange( int(iYearStart.value), int(iMonthStart.value), int(iDayStart.value) );
		}
		/** 更新日期 */
		ns_interactive_slot function startDayByYear( index:int ):void
		{
			var year:int = int(iYearStart.value);
			if ( iMonthStart.value == String(2) )
			{
				var day:Date = new Date( year, 1, 29 );
				if ( ( day.date == 29 && iDayStart.data.length < 29 ) ||
					 ( day.date < 29 && iDayStart.data.length == 29 ) )
				{
					startDayByMonth( iMonthStart.currentKey );
				}
			}
			adjustEndRange( int(iYearStart.value), int(iMonthStart.value), int(iDayStart.value) );
		}
		/** 选择了开始日期，调整结束日期为开始日期之后 */
		private function adjustEndRange( year:int, month:int, day:int ):void
		{
			iYearEnd.data = range( year+1, year ).join(",");
			endMonthByYear( iYearEnd.currentKey );
		}
		ns_interactive_slot function endMonthByYear( index:int ):void
		{
			// 把时间调整到开始日期之后
			var year_start:int = parseInt( iYearStart.value );
			var year_end:int   = parseInt( iYearEnd.value );
			ASSERT(year_start <= year_end );
			
			var month_start:int = parseInt( iMonthStart.value );
			if ( year_start == year_end )
			{
				iMonthEnd.data = range( 12, month_start ).join(",");
			}
			else iMonthEnd.data = range( month_start, 1 ).join(",");
			endDateByMonth( iMonthEnd.currentKey );
		}
		ns_interactive_slot function endDateByMonth( index:int ):void
		{
			// 把时间调整到开始日期之后
			var year_start:int = parseInt( iYearStart.value );
			var year_end:int   = parseInt( iYearEnd.value );
			
			var month_start:int = parseInt( iMonthStart.value );
			var month_end:int = parseInt( iMonthEnd.value );
			
			var day:Date = new Date( year_end, month_end, 1 );
			day.setTime( day.time - 1000 );
			if ( month_start == month_end )
			{
				if( year_start == year_end )
					iDayEnd.data = range( int(day.date), parseInt(iDayStart.value) ).join(",");
				else iDayEnd.data = range( parseInt(iDayStart.value), 1 ).join(",");
			}
			else
			{
				iDayEnd.data = range( day.date, 1 ).join(",");
			}
		}
		/** 打开日历页，更新日历数据 */
		ns_interactive_slot function setTimePanel( button:ui_button ):void
		{
			// 检查一下跨度有几个月
			var year_start:int = parseInt( iYearStart.value );
			var year_end:int = parseInt( iYearEnd.value );
			
			var month_start:int = parseInt( iMonthStart.value );
			var month_end:int = parseInt( iMonthEnd.value );
			
			const num_month_per_year:int = 12;
			var start:int = year_start * num_month_per_year + month_start;
			var end:int = year_end * num_month_per_year + month_end;
			var num_month:int = end - start;
			
			var day_start:int = parseInt( iDayStart.value );
			var day_end:int = parseInt( iDayEnd.value );
			
			var time_data:Array = [];
			var day:Date, value:Point = new Point();
			for ( var i:int = 0; i <= num_month; ++i )			//填充这几个月的数据到底是啥
			{
				var month:int = month_start - 1 + i;			//临时变量，辅助计算
				
				// 这个月的开始那天
				if ( i == 0 )
				{
					day = new Date( year_start, month_start-1, day_start );
				}
				else if( month > 11 )
				{
					day = new Date( year_start + 1, month % 12, 1 );
				}
				else 
				{
					day = new Date( year_start, month, 1 );
				}
				value.x = day.time;
				
				// 这个月的结束的那天
				if ( i == num_month )
				{
					day = new Date( year_end, month_end - 1, day_end );
					value.y = day.time;
				}
				else if ( month >= 11 )
				{
					day = new Date( year_start + 1, (1 + month) % 12, 1 );
					value.y = day.time - 1000;
				}
				else
				{
					day = new Date( year_start, month + 1, 1 );
					value.y = day.time - 1000;
				}
				
				// 计算完毕
				time_data.push( value.clone() );
			}
			DataMonitor.updateData( time_data, VNIC.EXEC_MONTHDATA );
			
			var day_data:Array = [];
			for each( var time:Point in time_data )
			{
				day.setTime( time.x );
				
				var off:int = day.day;
				value.x = day.date;
				
				day.setTime( time.y );
				value.y = day.date;
				
				time_data = range( value.y, value.x );
				time_data.offset = off;
				day_data.push( time_data );
			}
			DataMonitor.updateData( day_data, VNIC.EXEC_WEEKDATA );
			
			var nextPanel:Function = dialogProperty( this, "nextPanel", typeof(this) );
			nextPanel( button );
		}
		
		//交互组件定义
		//------------------------------------
		public function get iYearStart():ui_combo_box
		{
			return mFrame.iYearStart.component;
		}
		public function get iMonthStart():ui_combo_box
		{
			return mFrame.iMonthStart.component;
		}
		public function get iDayStart():ui_combo_box
		{
			return mFrame.iDayStart.component;
		}
		public function get iYearEnd():ui_combo_box
		{
			return mFrame.iYearEnd.component;
		}
		public function get iMonthEnd():ui_combo_box
		{
			return mFrame.iMonthEnd.component;
		}
		public function get iDayEnd():ui_combo_box
		{
			return mFrame.iDayEnd.component;
		}
		public function get iNextButton():ui_button
		{
			return mFrame.iNextButton.component;
		}
		public function get iCancelButton():ui_button
		{
			return mFrame.iCancelButton.component;
		}
	}

}